package com.example.jnnews;
//114514
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    private Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        EditText etUsername = findViewById(R.id.et_username);
        EditText etPassword = findViewById(R.id.et_password);
        Button btnLogin = findViewById(R.id.btn_login);
        TextView tvRegister = findViewById(R.id.tv_register);
        sharedPreferences = getSharedPreferences("jnnews_prefs", MODE_PRIVATE);

        // 清除之前的登录状态
        sharedPreferences.edit().remove("currentUser").apply();

        btnLogin.setOnClickListener(v -> {
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (validateInput(username, password)) {
                loginUser(username, password);
            }
        });

        tvRegister.setOnClickListener(v ->
                startActivityForResult(new Intent(this, RegisterActivity.class), 100)
        );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK && data != null) {
            String username = data.getStringExtra("username");
            EditText etUsername = findViewById(R.id.et_username);
            etUsername.setText(username);
            Toast.makeText(this, "注册成功，请输入密码登录", Toast.LENGTH_SHORT).show();
        }
    }

    private boolean validateInput(String username, String password) {
        if (username.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void loginUser(String username, String password) {
        List<User> users = getUsers();
        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                sharedPreferences.edit()
                        .putString("currentUser", username)
                        .apply();

                startActivity(new Intent(this, MainActivity.class));
                finish();
                return;
            }
        }
        Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
    }

    private List<User> getUsers() {
        String json = sharedPreferences.getString("users", "");
        if (json.isEmpty()) return new ArrayList<>();
        Type type = new TypeToken<ArrayList<User>>(){}.getType();
        return gson.fromJson(json, type);
    }
}